Setup External Resource
The book has now been published and the content of this chapter has likely changed substanstially.The system under test (SUT) depends on the contents of an external resource which is acting as a Mystery Guest in our test.
Create an external resource within the fixture setup logic of the test rather than using a predefined resource.
From [RTC]:
If it is necessary for a test to rely on external resources, such as directories, databases, or files, make sure the test that uses them explicitly creates or allocates these resources before testing, and releases them when done (take precautions to ensure the resource is also released when tests fail).
Implementation Notes
When our SUTmust use an external resources such as a file and we absolutely, positively cannot replace the access mechanism with a Test Stub (page X) or Fake Object (page X), we may need to live with the fact that we have to use an external resource. The problem with external resources is that the test reader cannot tell what they contain, they may disappear unexpectedly causing tests to fail because of Resource Optimism (see Erratic Test on page X) and they may result in Interacting Testss (see Erratic Test) and Test Run Wars (see Erratic Test). Setup External Resource does not help us with the last problem but it does avoid the problems of Mystery Guest (see Obscure Test on page X) and Resource Optimism.
To implement the Setup External Resource refactoring we simply pull the contents of the external resource into our Test Method (page X), setUp method or a Test Utility Method (page X) called by them. Using the contents we construct the external resource within our test code thereby making it evident to the test reader exactly what the test depends on. This also guarantees that the resource exists because we create it in every test run.
Copyright © 2003-2008 Gerard Meszaros all rights reserved